/**
 * 角色编辑窗口
 * @author linzongxue
 * @create_date 2012-1-7
 */
define(function(){
	var RoleWindow = Ext.extend(Ext.Window, {
		width: 400,
		resizable: false,
		modal: true,
	    animateTarget: cu.getDefaultAnimateTarget(),
        bodyStyle:'padding:3px;',
		iconCls: 'icon-window',
		buttonAlign: 'right',
		initComponent: function(){
			this.title = this.modifiedId ? '修改角色' : '添加角色';
			this.formPanel = this.createFormPanel();
			this.items = [this.formPanel];
			this.buttons = this.createButtons();
			RoleWindow.superclass.initComponent.call(this);
			this.initFormData();
			this.on('show', function(){
				this.firstFocus();
				cu.bindEnter(this, this.save);
			});
		},
		modifiedId: null, //从构造器中传入，表示要编辑的对象的id，为空表示新增
		initFormData: function(){
			if (!this.modifiedId) return;
			cu.get({
				url:'sys/role/get.do', 
				params: {id: this.modifiedId},
				success: function(data){
					this.getForm().setValues(data);
				},
				scope: this
			});
		},
		save: function(continueToAdd){
			cu.submit({
				form: this.getForm(),
				url: 'sys/role/save.do',
				success: function(data){
					if (this.callback){
						this.callback.call(this.cbScope, data);
					}
					if (continueToAdd){
						this.getField('name').setValue(null);
						this.getField('memo').setValue(null);
						this.getForm().clearInvalid();
						this.firstFocus();
					}
					else{
						this.close();
					}
				},
				scope: this
			})
		},
		createFormPanel: function(){
			var me = this;
			var formPanel = new Ext.form.FormPanel({
			    baseCls: 'x-plain',
			    bodyStyle:'padding:5px;',
			    labelWidth: 56,
			    labelPad: 5,
			    labelAlign: "right",
			    defaults: {anchor:'99%', style:'margin-bottom:5px;'},
			    items: [
			        {xtype:'hidden',name:'id'},
			        {
			        	xtype:'textfield',
			        	name: 'name',
			        	fieldLabel: '* 名称',
			        	maxLength: 30, 
			        	maxLengthText: '角色名称不能超过30个字',
			        	allowBlank:false
					},{
						xtype: 'textarea',
						name: 'memo',
						fieldLabel: '备注',
			        	maxLength: 500, 
			        	maxLengthText: '备注不能超过500个字'
					}
			    ]
			});
			return formPanel;
		},
		createButtons: function(){
			var buttons = [];
			if (!this.modifiedId){
				buttons.push({text: '保存并新增', width:90, handler: function(){this.save(true);}, iconCls: 'icon-save', scope: this});
			}
			buttons.push({text: '保存', width:60, handler: function(){this.save(false);}, iconCls: 'icon-save', scope: this});
			buttons.push({text: '关闭', width:60, handler: this.close, iconCls: 'icon-close', scope: this});
			return buttons;
		},
		getForm: function(){
			return this.formPanel.getForm();
		},
		getField: function(name){
			return this.getForm().findField(name);
		},
		firstFocus: function(){
			this.getField('name').focus(true, 500);
		}
	});
	return RoleWindow;
});